<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>三岁博客后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="icon" href="../../static/images/favicon.ico" th:href="@{/images/favicon.ico}"/>
    <link rel="stylesheet" href="../../../static/lib/admin/lib/layui-v2.6.3/css/layui.css" th:href="@{/lib/admin/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" href="../../../static/lib/admin/css/public.css" media="all" th:href="@{/lib/admin/css/public.css}">
    <link rel="stylesheet" href="../../../static/lib/editormd/css/editormd.min.css" th:href="@{/lib/editormd/css/editormd.min.css}"><!--MD编辑器css-->


</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>文章发布</legend>
        </fieldset>

        <form class="layui-form" action="" lay-filter="data-form">
            <div class="layui-form-item">
                <label class="layui-form-label">文章标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入文章标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">文章来源</label>
                <div class="layui-input-block">
                    <select name="sourceFlag" lay-verify="sourceFlag" lay-filter="aihao">
                        <option value=""></option>
                        <option value="0" selected="">原创</option>
                        <option value="1">转载</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">文章分类</label>
                    <div class="layui-input-inline">
                        <input name="typesId" id="typesId" type="hidden" value=''/>
                        <input type="text" name="typesName" lay-verify="type" placeholder="请输入文章分类" autocomplete="off" class="layui-input"
                               id="typesName">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">文章标签</label>
                    <div class="layui-input-inline">
                        <input name="tagsId" id="tagsId" type="hidden" value=''/>
                        <input type="text" name="tagsName" lay-verify="tag" placeholder="请输入文章标签" autocomplete="off" class="layui-input"
                               id="tagsName">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">文章封面</label>
                <div class="layui-input-block">
                    <input type="text" name="blogImg" lay-verify="blogImg" autocomplete="off" placeholder="请输入文章封面"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <input type="checkbox" name="shareStatement" title="转载声明"  checked="">
                    <input type="checkbox" name="appreciation" title="赞赏">
                    <input type="checkbox" name="commentabled" title="评论" checked="">
                    <input type="checkbox" name="recommend" title="推荐">
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <div class="required stackable field">
                    <div id="md-content" style="z-index: 1 !important;">
                        <textarea placeholder="文章内容" lay-verify="content" name="content" style="display: none"></textarea>
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">文章摘要</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" lay-verify="description" name="description" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-normal" name="status" lay-submit="" lay-filter="save">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script><!--引入jquery js-->
<script src="../../../static/lib/editormd/editormd.min.js" th:src="@{/lib/editormd/editormd.min.js}"></script><!--MD编辑器JS-->
<script src="../../static/lib/admin/lib/layui-v2.6.3/layui.js" th:src="@{/lib/admin/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script src="../../static/lib/admin/js/lay-config.js?v=1.0.4" th:src="@{/lib/admin/js/lay-config.js?v=1.0.4}" charset="utf-8"></script>

<script th:inline="javascript">
    var contentEditor;
    contentEditor = editormd("md-content", {
        width: "100%",
        height: 640,
        syncScrolling: "single",
        path: /*[[@{/lib/editormd/lib/}]]*/ "../../../static/lib/editormd/lib/"
    });
</script>

<script>

    layui.use(['form', 'layedit', 'laydate', 'tableSelect'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , tableSelect = layui.tableSelect;

        tableSelect.render({
            elem: '#typesName',
            checkedKey: 'typeId',
            searchPlaceholder: '分类搜索',
            table: {
                url: '../../page/getTypeList/blogs',
                method: 'post',
                cols: [
                    [
                        { type: 'radio' },
                        { field: 'typeId', title: 'ID' },
                        { field: 'typeName', title: '分类名称' },
                    ]
                ]
            },
            done: function (elem, data) {
                var NEWJSON = []
                var typesId = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.typeName)
                    typesId.push(item.typeId)
                })
                elem.val(NEWJSON.join(","));
                $("#typesId").val(typesId.join(","));
            }
        });


        tableSelect.render({
            elem: '#tagsName',
            checkedKey: 'tagId',
            searchPlaceholder: '标签搜索',
            table: {
                url: '../../page/getTagList/blogs',
                method: 'post',
                cols: [
                    [
                        { type: 'checkbox' },
                        { field: 'tagId', title: 'ID' },
                        { field: 'tagName', title: '标签名称' },
                    ]
                ]
            },
            done: function (elem, data) {
                var NEWJSON = []
                var tagsId = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.tagName)
                    tagsId.push(item.tagId)
                })
                elem.val(NEWJSON.join(","))
                $("#tagsId").val(tagsId.join(","))
            }
        });

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value == null || value == '') {
                    return '请输入文章标题啊，喂';
                }
            }
            ,
            sourceFlag: function (value) {
                if ((value != 1 && value != 0) || value == null || value == '') {
                    return '请选择文章来源';
                }
            }
            ,
            type: function(value){
                if (value == null || value == '') {
                    return '哎呀，分类还没选呢';
                }
            }
            ,
            tag: function(value){
                if (value == null || value == '') {
                    return '哦吼，不打算选一个标签嘛';
                }
            }
            ,
            content: function (value) {
                // console.log(value)
                if (value == null || value == '') {
                    return '宝，不打算说点什么嘛';
                }
            }
        });

        //表单初始赋值
        $("#typesName").attr("ts-selected",window.parent.formData.typesId);
        $("#tagsName").attr("ts-selected",window.parent.formData.tagsId);

        form.val('data-form',{
            'title': window.parent.formData.title,
            'sourceFlag': window.parent.formData.sourceFlag,
            'typesId': window.parent.formData.typesId,
            'typesName': window.parent.formData.typesName,
            'tagsId': window.parent.formData.tagsId,
            'tagsName': window.parent.formData.tagsName,
            'blogImg': window.parent.formData.blogImg,
            'shareStatement': window.parent.formData.shareStatement,
            'appreciation': window.parent.formData.appreciation,
            'commentabled': window.parent.formData.commentabled,
            'recommend': window.parent.formData.recommend,
            'content': window.parent.formData.content,
            'description': window.parent.formData.description
        })

        //监听保存
        form.on('submit(save)', function (data) {
            data.field.appreciation = (data.field.appreciation == 'on')? 1:0;
            data.field.shareStatement = (data.field.shareStatement == 'on')? 1:0;
            data.field.commentabled = (data.field.commentabled == 'on')? 1:0;
            data.field.recommend = (data.field.recommend == 'on')? 1:0;
            data.field.blogId = window.parent.formData.blogId;
            data.field.status = window.parent.formData.status;

            // console.log(data.field)

            $.ajax({
                url: '../../page/blog/update',
                type: 'post',
                dataType: 'json',
                contentType:'application/json;charset=utf-8',
                data: JSON.stringify(data.field),
                success: function (suc) {
                    if (suc.code == 200) {
                        layer.alert(suc.msg, {icon: 1});
                        // layer.alert(suc.msg, {icon: 1,cancel: function(){
                                // var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                // parent.layer.close(index);
                                //刷新页面
                                // parent.location.reload();
                            // }}, function () {
                            // 获得frame索引
                            // var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            // parent.layer.close(index);
                            //刷新页面
                            // parent.location.reload();
                        // });
                    } else {
                        layer.alert(suc.msg, {icon: 5});
                    }
                }
            });
            return false;
        });

    });


</script>



</body>
</html>